<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>随机数生成器</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#3B82F6',
                        secondary: '#6366F1',
                        accent: '#8B5CF6',
                        neutral: '#1F2937',
                        'neutral-light': '#F3F4F6',
                    },
                    fontFamily: {
                        sans: ['Inter', 'system-ui', 'sans-serif'],
                    },
                }
            }
        }
    </script>
    <style type="text/tailwindcss">
        @layer utilities {
            .content-auto {
                content-visibility: auto;
            }
            .text-shadow {
                text-shadow: 0 2px 4px rgba(0,0,0,0.1);
            }
            .transition-custom {
                transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            }
        }
    </style>
</head>
<body class="bg-gradient-to-br from-blue-50 to-indigo-50 min-h-screen font-sans">
    <div class="container mx-auto px-4 py-12 max-w-3xl">
        <header class="text-center mb-12">
            <h1 class="text-[clamp(2rem,5vw,3.5rem)] font-bold text-neutral mb-4 text-shadow">
                随机数生成器
            </h1>
            <p class="text-gray-600 text-lg max-w-2xl mx-auto">
                轻松生成指定范围内的随机整数或小数，满足你的各种随机需求
            </p>
        </header>

        <main class="bg-white rounded-2xl shadow-xl p-8 mb-12 transform hover:shadow-2xl transition-custom">
            <div class="mb-8">
                <label for="min" class="block text-sm font-medium text-gray-700 mb-2">
                    最小值
                </label>
                <input type="number" id="min" value="1" 
                       class="w-full px-4 py-3 rounded-lg border border-gray-300 focus:ring-2 focus:ring-primary/50 focus:border-primary outline-none transition-custom">
            </div>

            <div class="mb-8">
                <label for="max" class="block text-sm font-medium text-gray-700 mb-2">
                    最大值
                </label>
                <input type="number" id="max" value="100" 
                       class="w-full px-4 py-3 rounded-lg border border-gray-300 focus:ring-2 focus:ring-primary/50 focus:border-primary outline-none transition-custom">
            </div>

            <div class="mb-8">
                <label class="block text-sm font-medium text-gray-700 mb-2">
                    随机数类型
                </label>
                <div class="flex flex-wrap gap-4">
                    <label class="inline-flex items-center">
                        <input type="radio" name="type" value="integer" checked 
                               class="w-4 h-4 text-primary focus:ring-primary">
                        <span class="ml-2">整数</span>
                    </label>
                    <label class="inline-flex items-center">
                        <input type="radio" name="type" value="decimal" 
                               class="w-4 h-4 text-primary focus:ring-primary">
                        <span class="ml-2">小数</span>
                    </label>
                </div>
            </div>

            <div class="mb-8">
                <label for="decimals" class="block text-sm font-medium text-gray-700 mb-2">
                    小数位数 (仅适用于小数)
                </label>
                <input type="number" id="decimals" value="2" min="1" max="10" 
                       class="w-full px-4 py-3 rounded-lg border border-gray-300 focus:ring-2 focus:ring-primary/50 focus:border-primary outline-none transition-custom">
            </div>

            <button id="generate" 
                    class="w-full bg-gradient-to-r from-primary to-secondary text-white py-3 px-6 rounded-lg font-medium shadow-lg hover:shadow-xl hover:from-primary/90 hover:to-secondary/90 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary transform hover:-translate-y-0.5 transition-custom flex items-center justify-center">
                <i class="fa fa-random mr-2"></i>
                生成随机数
            </button>

            <div id="result" class="mt-8 text-center hidden">
                <div class="text-2xl font-bold text-neutral mb-2">结果</div>
                <div id="result-value" class="text-[clamp(1.5rem,3vw,2.5rem)] font-bold text-primary py-4 px-6 rounded-xl bg-primary/5 inline-block min-w-[200px]"></div>
            </div>
        </main>

        <footer class="text-center text-gray-500 text-sm">
            <p>© 2025 随机数生成器 | 使用现代网页技术构建</p>
        </footer>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const minInput = document.getElementById('min');
            const maxInput = document.getElementById('max');
            const typeRadios = document.querySelectorAll('input[name="type"]');
            const decimalsInput = document.getElementById('decimals');
            const generateButton = document.getElementById('generate');
            const resultDiv = document.getElementById('result');
            const resultValue = document.getElementById('result-value');

            // 禁用/启用小数位数输入框
            typeRadios.forEach(radio => {
                radio.addEventListener('change', function() {
                    if (this.value === 'decimal') {
                        decimalsInput.disabled = false;
                    } else {
                        decimalsInput.disabled = true;
                    }
                });
            });

            // 生成随机数的函数
            function generateRandomNumber() {
                const min = parseFloat(minInput.value);
                const max = parseFloat(maxInput.value);
                let selectedType = 'integer';
                
                typeRadios.forEach(radio => {
                    if (radio.checked) {
                        selectedType = radio.value;
                    }
                });

                // 验证输入
                if (isNaN(min) || isNaN(max)) {
                    alert('请输入有效的最小值和最大值');
                    return;
                }
                
                if (min >= max) {
                    alert('最小值必须小于最大值');
                    return;
                }

                let randomNum;
                
                if (selectedType === 'integer') {
                    // 生成随机整数
                    randomNum = Math.floor(Math.random() * (max - min + 1)) + min;
                } else {
                    // 生成随机小数
                    randomNum = Math.random() * (max - min) + min;
                    const decimals = parseInt(decimalsInput.value);
                    randomNum = randomNum.toFixed(decimals);
                }

                // 显示结果
                resultValue.textContent = randomNum;
                resultDiv.classList.remove('hidden');
                
                // 添加动画效果
                resultDiv.classList.add('animate-bounce');
                setTimeout(() => {
                    resultDiv.classList.remove('animate-bounce');
                }, 1000);
            }

            // 添加生成按钮点击事件
            generateButton.addEventListener('click', generateRandomNumber);

            // 添加键盘事件，按Enter键生成随机数
            document.addEventListener('keypress', function(event) {
                if (event.key === 'Enter') {
                    generateRandomNumber();
                }
            });
        });
    </script>
</body>
</html>
    